import BasePage from 'src/pageP/base/base-page'
import { StyleSheet } from 'react-native'
import { postRecommendList } from 'src/utils/RequestAPI'
import CustomFlatList from 'src/components/custom-flat-list'
import React from 'react'
import CustomButton from 'src/components/button/CustomButton'
import RefreshState from 'src/components/custom-flat-list/RefreshState'
import { DismissLoading, ShowLoading } from 'src/utils/ToastUtil'
import JobCell from 'src/components/job-cell'
import { connect } from 'react-redux'
import NavigationUtil from 'src/utils/NavigationUtil'

@connect(
  ({ landmark }) => ({
    landmark
  })
)
class RecommendList extends BasePage {
  constructor (props) {
    super(props)
    this.state = {
      header: {},
      dataArr: []
    }
  }

  childRender () {
    const { dataArr } = this.state
    return (
      <CustomFlatList
        style={styles.container}
        ref={this._customFlatListRef}
        data={dataArr}
        renderItem={this._renderItem}
        onHeaderRefresh={this._onHeaderRefresh}
        pullUp={false}
        keyExtractor={this._keyExtractor}
      />
    )
  }

  componentDidMount () {
    this._postRecommendList(true)
  }

  _customFlatListRef = (ref) => {
    this.cflr = ref
  }

  _keyExtractor = (item) => {
    return item.id.toString()
  }

  /**
   * 岗位推荐列表
   * @private
   */
  _postRecommendList = (showLoading) => {
    showLoading && ShowLoading()
    postRecommendList().then((data) => {
      console.info('postRecommendList', data)
      showLoading && DismissLoading()
      this.setState({ dataArr: data.data })
      this._setEndRefreshing(RefreshState.NoMoreData)
    }).catch(() => {
      showLoading && DismissLoading()
      this._setEndRefreshing(RefreshState.Idle)
    })
  }

  _setEndRefreshing = (state) => {
    this.cflr.endRefreshing(state)
  }

  /**
   * 渲染每一个
   * @private
   */
  _renderItem = ({ item }) => {
    const { landmark } = this.props

    return (
      <CustomButton onPress={this._itemClick} params={item}>
        <JobCell item={item} landmark={landmark}/>
      </CustomButton>
    )
  }

  /**
   * 每一条点击
   * @private
   */
  _itemClick = (item) => {
    NavigationUtil.navigate('CJobdetail', { id: item.id })
  }

  /**
   * 下拉刷新
   * @private
   */
  _onHeaderRefresh = () => {
    this._postRecommendList()
  }
}

export default RecommendList
const styles = StyleSheet.create({
  container: {
    flex: 1
  }
})
